package com.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Paper;
import com.demo.entity.Treatise;
import com.demo.mapper.TreatiseMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 论著信息表
 */
@RestController
@RequestMapping("/treatise")
public class TreatiseController {
  /**
   * 服务对象
   */
  @Resource
  TreatiseMapper treatiseMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Treatise treatise) {
    System.out.println(treatise);
    try {
      treatiseMapper.insert(treatise);
      return Result.success();
    } catch (Exception e) {
      System.out.println(e);
      return Result.error("201", "");
    }
    
  }
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    treatiseMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody Treatise treatise) {
    try {
      treatiseMapper.updateById(treatise);
      return Result.success();
    } catch (Exception e) {
      System.out.println(e);
      return Result.error("201", "");
    }
   
  }
  
  /**
   * 分页,默认1页,10个数据,  默认根据 Tname 著作名称  查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @param sort     搜索类别
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "Tname") String sort) {
    LambdaQueryWrapper<Treatise> wrapper = Wrappers.<Treatise>lambdaQuery();
    //根据Id逆序排列
    wrapper.orderByDesc(Treatise::getId);
    if (StrUtil.isNotBlank(search)) {
      switch (sort) {
        case "tWcategory": //著作类别
          wrapper.like(Treatise::getWcategory, search);
          break;
        case "Tname"://著作名称
          wrapper.like(Treatise::getTname, search);
          break;
        case "Isbn"://图书编号
          wrapper.like(Treatise::getIsbn, search);
          break;
        case "Scategory"://学科类别
          wrapper.like(Treatise::getScategory, search);
          break;
        case "Author"://作者
          wrapper.like(Treatise::getAuthor, search);
          break;
        case "Pno"://职工号
          wrapper.like(Treatise::getPno, search);
          break;
      }
    }
    
    Page<Treatise> AdminPage = treatiseMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(AdminPage);
  }
  
  
  /**
   * 查询数据库中是否已经有主键了
   */
  @GetMapping("/isbn/{isbn}")
  public Result<?> getIsbn(@PathVariable String isbn) {
    Treatise i = treatiseMapper.selectOne(Wrappers.<Treatise>lambdaQuery().eq(Treatise::getIsbn, isbn));
    if (i == null) {
      //表示不含有,则可以输入
      return Result.success();
    }
    //表示表中已存在,则不可以输入
    return Result.errorData("201", i);
  }
  
  
}

